#include "server.hpp"

class EchoServer 
{
private:
    TcpServer _tcp;
private:
    void OnConnected(const PtrConnection &conn)
    {
        DBG_LOG("NEW CONNECTION:%p", conn.get());
    }
    void OnClosed(const PtrConnection &conn)
    {
        DBG_LOG("CLOSE CONNECTION:%p", conn.get());
    }
    void OnMessage(const PtrConnection &conn, Buffer *buf)
    {
        conn->Send(buf->GetReadPos(), buf->ReadIndexSize());
        buf->MoveReadOff(buf->ReadIndexSize());
        conn->Shutdown();
    }

public:
    EchoServer(int port) : _tcp(port)
    {
        _tcp.SetThreadCount(2);
        _tcp.EnableInactiveRelease(10);
        _tcp.SetClosedCallback(std::bind(&EchoServer::OnClosed, this, std::placeholders::_1));
        _tcp.SetConnectedCallback(std::bind(&EchoServer::OnConnected, this, std::placeholders::_1));
        _tcp.SetMessageCallback(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
    }
    void Start() { _tcp.Start(); }
};